package com.soho.helper;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class SqlHelper {

	//获取指定表中最后的Time字段,用于更新时间
	public static  long getLastTime(String symbole,String table,long defaultValue) {
		Record row=  Db.findFirst("select Time from "+table+" where Symbol=? order by Time desc limit 1",symbole);
		if(UtilsHelper.checkIsEmpty(row)) {
			return defaultValue;
		}
		return TimeHelper.getSecondFromStr(row.getStr("Time"));
	}
	
	public static List<Record> getQuotesList(String table,String symbol,String time,int limit){
		return Db.find("select * from "+table+" where Symbol=? and Time<=? order by Time desc limit ? ", symbol,time,limit);
	}
	
	//获取一条数据
	public static Record getRecord(String table,String symbol,String time) {
		return Db.findFirst("select * from "+table+" where Symbol=? and Time=?", symbol,time);
	}
	//更新一条数据
	public static void updateRecord(String table,Record record) {
		Db.update(table, record);
	}
	
	public static void saveRecord(String table,Record record) {
		Db.save(table, record);
	}
	
	public static List<String> getTimes(String table,String symbole,int limit){
		List<Record> dbLists=Db.find("select Time from "+table+" where Symbol=? order By Time desc limit ?", symbole,limit);
		List<String> timeLst=new ArrayList<>();
		
		dbLists.forEach((rv)->{
			timeLst.add(rv.getStr("Time"));
		});
		
		return timeLst;
	}
}
